import java.util.*;


/**
 * Created by IntelliJ IDEA.
 * User: ReZzZ
 * Date: 07 Apr 11
 * Time: 12:11:19
 * To change this template use File | Settings | File Templates.
 */
public class GridView {
    final int X = 10;
    private ArrayList<Car> carArray = new ArrayList<Car>();
    TrafficLight tl = new TrafficLight();
    ProjModel pm = new ProjModel();
    final int HorGrid = 25;
    final int VerGrid = 26;
     char[][]cell = new char[HorGrid][VerGrid];
    int horLanes = pm.getHorLanes() ;
    int verLanes = pm.getVerLanes() ;
//    double horProb = pm.getHorCarProb() ;
//    double verProb = pm.getVerCarProb() ;


    public char [][] getGridView(int horLanes, int verLanes ){

        final int HorGrid = 25;
        final int VerGrid = 26;
        final int X = 10;
        char[][]cell = new char[HorGrid][VerGrid];

      return cell;
     }

     public void initGrid(ProjModel model){
        for (int a = 0; a<HorGrid;a++){
            for (int b=0 ; b <VerGrid;b++ ){
              cell [a][b]= ' ';
            }
        }
        for (int a =0; a < X ; a++){
            cell[X][a] = '-' ;
            cell[X][X+1+a+model.getHorLanes()+1+1]='-';
            cell[X+model.getHorLanes()+1][a]='-';
            cell[X+model.getHorLanes()+1][X+model.getVerLanes()+1+a+1]='-';
        }
         for (int a =0; a < X ; a++){
            cell[a][X] = '|' ;
            cell[a][X+1+model.getVerLanes()]= '|';
            cell[X+model.getHorLanes()+1+1+a][X]='|';
            cell[X+model.getHorLanes()+1+1+a][X+model.getVerLanes()+1]='|';
         }
     }

     public void gridPrint(char[][] cell){
         final int HorGrid = 25;
         final int VerGrid = 26;
         for (int a=0 ; a < HorGrid; a++){
             for (int b=0 ; b< VerGrid ; b++) {
                 System.out.print(cell[a][b]);
             }
             System.out.println();
         }
     }

      public char [][] buildHorCars(double horProb, int horLanes) {

        Random rnd = new Random();
        float x = rnd.nextFloat();

        if (horProb >= x) {
            if (horLanes == 1) {
                carArray.add(new Car(11, 0, 'r'));
            }
            else if (horLanes == 2) {
                carArray.add(new Car((11 + (int) (Math.random() * 2)), 0, 'r'));
            }
            else if (horLanes == 3) {
                carArray.add(new Car((11 + (int) (Math.random() * 3)), 0, 'r'));
            }
        }
        return cell;
      }
     public char [][] buildVerCars(double verProb, int verLanes) {

        Random rnd = new Random();
        float x = rnd.nextFloat();
        if (verProb >= x) {
            if (verLanes == 1) {
                carArray.add(new Car(0, 11, 'd'));
            }
            else if (verLanes == 2) {
                carArray.add(new Car(0, (11 + (int) (Math.random() * 2)), 'd'));
            }
            else if (verLanes == 3) {
                carArray.add(new Car(0, (11 + (int) (Math.random() * 3)), 'd'));
            }
            else if (verLanes == 4) {
                carArray.add(new Car(0, (11 + (int) (Math.random() * 4)), 'd'));
            }


        }//end if
      return cell;
    }
    public void insertCars() {
        for (int i = 0; i < carArray.size(); i = i + 1) {
            int x = carArray.get(i).getXcor();
            int y = carArray.get(i).getyCor();
            cell[x][y] = 'c';
        }
    }

    public void trafficControl() {


        for (int count = 0; count < carArray.size(); count = count + 1) {


            if (carArray.get(count).getDirection() == 'd') {
                if (tl.getVerRed() == true) {
                    cell[10][10] = 'R';
                    if (carArray.get(count).getXcor() < 10) {
                        eraseTrails(count);
                        moveHor(count);
                    }
                    if (carArray.get(count).getXcor() > 10) {
                        eraseTrails(count);
                        moveHor(count);
                    }

                } else if (tl.getVerRed() == false) {
                    cell[10][10] = 'G';
                    eraseTrails(count);
                    moveHor(count);
                }


            }//end if direction 'd'

            if (carArray.get(count).getDirection() == 'r') {
                if (tl.getHorRed() == true) {

                    cell[(15)][9] = 'R';
                    if (carArray.get(count).getyCor() < 10) {
                        eraseTrails(count);
                        moveVer(count);
                    }
                    if (carArray.get(count).getyCor() > 10) {
                        eraseTrails(count);
                        moveVer(count);
                    }

                } else if (tl.getHorRed() == false) {

                    cell[15][9] = 'G';
                    eraseTrails(count);
                    moveVer(count);
                }

            }//end if direction r

        }//end for


    }//end method

    public void moveHor(int cycle2) {
        carArray.get(cycle2).setXcor((carArray.get(cycle2).getXcor()) + 1); //add x coordinate 1 step

        int x = carArray.get(cycle2).getXcor();
        int y = carArray.get(cycle2).getyCor();
        if (x > 25) {
            carArray.remove(cycle2);
        } else {
            cell[x][y] = 'c';
        }
    }

    public void moveVer(int cycle3) {
        carArray.get(cycle3).setYcor((carArray.get(cycle3).getyCor()) + 1); // add y coordinate 1 step
        int x2 = carArray.get(cycle3).getXcor();
        int y2 = carArray.get(cycle3).getyCor();
        if (y2 > 25) {
            carArray.remove(cycle3);
        } else {
            cell[x2][y2] = 'c';
        }
    }

     public void runCycle(ProjModel model, int round) {

        model.setCycle(round);
        int counter = model.getCycle();

        for (int i=0; i < round; i++) {

            getGridView(model.getHorLanes(), model.getVerLanes());
            cell = buildHorCars(model.getHorCarProb(), model.getHorLanes());
            cell = buildVerCars(model.getVerCarProb(), model.getVerLanes());
            tl.trafficShuffle(i);
            gridPrint(cell);
            trafficControl();
            System.out.println(" ");
        }
     }
    public void eraseTrails(int cntr) {
        int originalX = carArray.get(cntr).getXcor();
        int originalY = carArray.get(cntr).getyCor();
        cell[originalX][originalY] = ' '; //erase car trail
    }
}
